package com.easygem.business.service.base;

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.List;


public class AbstractServiceImpl<M extends tk.mybatis.mapper.common.BaseMapper<T> & tk.mybatis.mapper.common.special.InsertListMapper<T> & tk.mybatis.mapper.common.IdsMapper<T>, T, PK> implements IBaseService<M, T, PK> {

    @Autowired
    protected M mapper;

    @Override
    public T selectByPrimaryKey(PK pk) {
        return mapper.selectByPrimaryKey(pk);
    }

    @Override
    public int selectCount(T record) {
        return mapper.selectCount(record);
    }

    @Override
    public T selectOne(T record) {
        return mapper.selectOne(record);
    }

    @Override
    public List<T> select(T record) {
        return mapper.select(record);
    }

    @Override
    public List<T> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public List<T> selectByIds(String ids) {
        return mapper.selectByIds(ids);
    }

    @Override
    public boolean existsWithPrimaryKey(PK pk) {
        return mapper.existsWithPrimaryKey(pk);
    }

    @Override
    public int insert(T record) {
        return mapper.insert(record);
    }

    @Override
    public int insertSelective(T record) {
        return mapper.insertSelective(record);
    }

    @Override
    public int insertList(List<? extends T> list) {
        final int partitionSize = 1000;

        if (list.size() <= partitionSize * 1.25) {
            return mapper.insertList(list);
        }

        final List<? extends List<? extends T>> partitions = Lists.partition(list, partitionSize);
        return partitions.stream().mapToInt(mapper::insertList).sum();
    }

    @Override
    public int updateByPrimaryKeySelective(T record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(T record) {
        return mapper.updateByPrimaryKey(record);
    }

    @Override
    public int deleteByPrimaryKey(PK pk) {
        return mapper.deleteByPrimaryKey(pk);
    }

    @Override
    public int delete(T record) {
        return mapper.delete(record);
    }

    /**
     * 根据主键字符串进行删除，类中只有存在一个带有@Id注解的字段
     *
     * @param ids 如 "1,2,3,4"
     * @return
     */
    @Override
    public int deleteByIds(String ids) {
        return mapper.deleteByIds(ids);
    }
}

